#ifndef NETWORK_ESSENTIAL_H
#define NETWORK_ESSENTIAL_H

#include <Winsock.h>

typedef int socklen_t;

bool sendData(SOCKET s, const char *buf, int size);
bool recvData(SOCKET s, char *buf, int size);

bool sendDataTo(SOCKET s, const char *buf, int size, sockaddr *toAddress, socklen_t length);
bool recvDataFrom(SOCKET s, char *buf, int size, sockaddr *fromAddress, socklen_t length);

template <typename Type>
char *FourBytesToNetChar(Type &value)
{
	value = htonl(value);
	return reinterpret_cast<char *> (&value);
}

template <typename Type>
Type NetCharToFourBytes(char *netchar)
{
	Type *pValue = reinterpret_cast<Type *> (netchar);
	return ntohl(*pValue);
}

template <typename Type>
Type RecvFourBytesFromNet(SOCKET s)
{
	char buf[4] = {0};
	recvData(s, buf, 4);
	Type *pValue = reinterpret_cast<Type *> (buf);
	return ntohl(*pValue);
}

template <typename Type>
Type RecvFourBytesFromNetFrom(SOCKET s, sockaddr *fromAddress, socklen_t length)
{
	char buf[4] = {0};
	recvDataFrom(s, buf, 4, fromAddress, length);
	Type *pValue = reinterpret_cast<Type *> (buf);
	return ntohl(*pValue);
}

#endif

